﻿using System;
using System.Linq;
using PicoContainer;

namespace XEvolve.Core.Runtime.Internal
{
    class PicoBasedExtensionPoint : IExtensionPoint
    {
        private readonly IMutablePicoContainer container;

        public PicoBasedExtensionPoint(IMutablePicoContainer container)
        {
            this.container = container;
        }

        public object[] ExtensionIdentifiers
        {
            get { return container.ComponentAdapters.Cast<IComponentAdapter>().Select(adapter => adapter.ComponentKey).ToArray(); }
        }

        public object[] Extensions
        {
            get { return container.ComponentInstances.Cast<object>().ToArray(); }
        }

        public object GetExtension(object extensionIdentifier)
        {
            return container.GetComponentInstance(extensionIdentifier);
        }

        internal void RegisterExtension(object identifier, object instance)
        {
            container.RegisterComponentInstance(identifier, instance);
        }

        internal void RegisterExtension(object identifier, Type implementation)
        {
            container.RegisterComponentImplementation(identifier, implementation);
        }
    }
}
